import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MovieManager {
    private static List<Movie> movies = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public void listAllMovies() {
        System.out.println("=== 影片列表 ===");
        for (Movie movie : movies) {
            System.out.println("片名: " + movie.getName() +
                    " 导演: " + movie.getDirector() +
                    " 主演: " + movie.getActor() +
                    " 剧情简介: " + movie.getIntro() +
                    " 时长(分钟): " + movie.getDuration() +
                    " 评分: " + movie.getScore());
        }
    }

    public void addMovie(User manager) {
        System.out.print("请输入片名: ");
        String name = scanner.nextLine();
        System.out.print("请输入导演: ");
        String director = scanner.nextLine();
        System.out.print("请输入主演: ");
        String actor = scanner.nextLine();
        System.out.print("请输入剧情简介: ");
        String intro = scanner.nextLine();
        System.out.print("请输入时长(分钟): ");
        int duration = getValidIntegerInput();
        System.out.print("请输入评分(0 - 10): ");
        double score = scanner.nextDouble();
        scanner.nextLine();

        movies.add(new Movie(name, director, actor, intro, duration, score));
        System.out.println("影片添加成功");
        logOperation(manager.getUsername(), "添加影片" + name, new java.util.Date());
    }

    public Movie findMovieByName(String name) {
        for (Movie movie : movies) {
            if (movie.getName().equals(name)) {
                return movie;
            }
        }
        return null;
    }

    public void deleteMovie(User manager, String name) {
        Movie movie = findMovieByName(name);
        if (movie == null) {
            System.out.println("影片不存在");
            return;
        }

        System.out.print("确定删除? (y/n): ");
        if (scanner.nextLine().equals("y")) {
            movies.remove(movie);
            System.out.println("影片删除成功");
            logOperation(manager.getUsername(), "删除影片" + name, new java.util.Date());
        }
    }

    public void modifyMovie(User manager, String name) {
        Movie movie = findMovieByName(name);
        if (movie == null) {
            System.out.println("影片不存在");
            return;
        }

        System.out.println("1. 修改评分");
        System.out.print("选择操作: ");
        int choice = getValidIntegerInput();

        if (choice == 1) {
            System.out.print("请输入新评分(0 - 10): ");
            double newScore = scanner.nextDouble();
            scanner.nextLine();
            movie.setScore(newScore);
            System.out.println("评分修改成功");
            logOperation(manager.getUsername(), "修改影片" + name + "评分", new java.util.Date());
        }
    }

    private int getValidIntegerInput() {
        while (true) {
            try {
                return Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的整数");
            }
        }
    }

    private void logOperation(String operator, String action, java.util.Date time) {
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String log = sdf.format(time) + " - 操作人: " + operator + " - 操作: " + action;
        System.out.println("[日志] " + log);
    }
}
